<?php

/**
 * @version 2.0
 * @author Bendsoft
 * @package Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 * @copyright (C) 2010 Bendsoft 
 *
 */
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_COMPILE_ERROR);
ini_set('display_errors', '1');

include 'settings.php';

mysql_connect(constant('DATABASE_HOST') . ":" . constant('DATABASE_PORT'), constant('DATABASE_USER'), constant('DATABASE_PASSWORD')) or die('Error connecting to mysql ' . mysql_error());
mysql_select_db(constant('DATABASE_NAME')) or die('Could not connect to database ' . mysql_error());


/**
 * Fix for PHP/Apache Querystrings, they are a real problem child
 */
if ($_GET)
    $xml = urldecode(substr($_SERVER["REQUEST_URI"], strlen($_SERVER['PHP_SELF']) + 9));

try {
    /**
     * Fetch data with form post, if no form post is found we try with GET (querystring)
     */
    $camelot_xml = ($_POST['xmldata']) ? $_POST['xmldata'] : $xml;
    
    $remote_shared_key = ($_POST['shared_key']) ? $_POST['shared_key'] : $_GET['shared_key'];
    $local_shared_key = md5(constant("POST_SHARED_KEY"));

    if ($remote_shared_key == $local_shared_key) {
        
        $sql = "INSERT INTO " . constant("DATABASE_PREFIX") . "camelot_dump SET camelot_dump_packet = '$camelot_xml',
            camelot_dump_ip = '" . constant('IP') . "',
            camelot_dump_date = '" . constant('NOW') . "',
            camelot_dump_packet_md5 = MD5('" . $camelot_xml . "')";

        mysql_query($sql) or die('Could not execute query ' . mysql_error());
        echo "Success!";
    } else {
        echo "Authentication error";
    }
    
} catch (Exception $exc) {
    
    echo $exc->getTraceAsString();
    
}
?>